// =================================================================================================
//
//	CopyEngine Framework
//	Copyright 2012 Eran. All Rights Reserved.
//
//	This program is free software. You can redistribute and/or modify it
//	in accordance with the terms of the accompanying license agreement.
//
// =================================================================================================

package demo
{
    import common.MoveThumbComponent;

    import flash.display.BitmapData;
    import flash.display.MovieClip;
    import flash.geom.Rectangle;

    import starling.core.Starling;

    public class DemoViewport
    {
        private var mGUIMc:MovieClip;
        private var mDemoAreaMc:MovieClip;

        private var mStarling:Starling;
        private var mStarlingViewPort:Rectangle;

        private var mHorizontalThumb:MoveThumbComponent;
        private var mVerticalThumb:MoveThumbComponent;

        public function DemoViewport()
        {
        }

        //==================================
        //== Initialize & Dispose
        //==================================

        public function initialize(_guiMc:MovieClip):void
        {
            mGUIMc = _guiMc;
            mDemoAreaMc = mGUIMc["demoAreaMc"];
            mStarlingViewPort = new Rectangle(mDemoAreaMc.x,
                                              mDemoAreaMc.y,
                                              mDemoAreaMc["displayAreaMc"].width,
                                              mDemoAreaMc["displayAreaMc"].height);

            mStarling = new Starling(StarlingDemoScene, mGUIMc.stage, mStarlingViewPort);
            mStarling.start();


            mHorizontalThumb = new MoveThumbComponent();
            mHorizontalThumb.initialize(mDemoAreaMc["horizontalThumb"],
                                        MoveThumbComponent.THUMB_TYPE_HORIZONTAL,
                                        onThumbMove);

            mVerticalThumb = new MoveThumbComponent();
            mVerticalThumb.initialize(mDemoAreaMc["verticalThumb"],
                                      MoveThumbComponent.THUMB_TYPE_VERTICAL,
                                      onThumbMove);
        }

        //==================================
        //== Public Function
        //==================================

        public function onBitmapTextureUpdate(_topLeft:BitmapData,
                                              _topRight:BitmapData,
                                              _bottomLeft:BitmapData,
                                              _bottomRight:BitmapData,
                                              _middle:BitmapData,
                                              _topMiddle:BitmapData,
                                              _leftMiddle:BitmapData,
                                              _rightMiddle:BitmapData,
                                              _bottomMiddle:BitmapData):void
        {
            (mStarling.root as StarlingDemoScene).onBitmapTextureUpdate(_topLeft,
                                                                        _topRight,
                                                                        _bottomLeft,
                                                                        _bottomRight,
                                                                        _middle,
                                                                        _topMiddle,
                                                                        _leftMiddle,
                                                                        _rightMiddle,
                                                                        _bottomMiddle);
        }

        //==================================
        //== Private Function
        //==================================

        private function onThumbMove():void
        {
            (mStarling.root as StarlingDemoScene).refreshPanelWidthAndHeight(mHorizontalThumb.getValue(),
                                                                             mVerticalThumb.getValue());
        }

    }
}
